<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addRole"
          >新增角色</el-button>
        </el-row>
        <!-- 新增弹框 -->
        <el-dialog :title="roleForm.id?'修改角色':'添加角色'" :visible="showDialog" @close="closeDialog">
          <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="roleForm.name" />
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="roleForm.description" />
            </el-form-item>
          </el-form>
          <!-- 底部 -->
          <el-row slot="footer" type="flex" justify="center">
            <el-col :span="6">
              <el-button size="small" @click="closeDialog">取消</el-button>
              <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            </el-col>
          </el-row>
        </el-dialog>
        <!-- 表格 -->
        <el-table :data="list">
          <el-table-column label="序号" width="120" type="index" />
          <el-table-column label="角色名称" width="240" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button size="small" type="success">分配权限</el-button>
              <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            layout="prev,pager,next"
            :total="total"
            :page-size="params.pagesize"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/setting'
export default {
  data() {
    return {
      list: [],
      params: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.hgetRoleList()
  },
  methods: {
    // 获取角色列表
    async hgetRoleList() {
      const res = await getRoleList(this.params)
      this.list = res.rows
      this.total = res.total
    },
    // 切换页码  刷新角色列表
    pageChange(page) {
      this.params.page = page
      this.hgetRoleList()
    },
    // 添加角色
    addRole() {
      this.showDialog = true
    },
    // 删除角色
    delRole(id) {
      this.$confirm('是否确认删除该角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 删除部门
        await deleteRole(id)
        // 页码优化
        this.list.length === 1 && this.params.page > 1 && this.params.page--
        // 刷新列表
        this.hgetRoleList()
        // 提示
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    },
    // 修改角色
    async editRole(id) {
      this.showDialog = true
      const res = await getRoleDetail(id)
      this.roleForm = res
    },
    // 关闭弹窗
    closeDialog() {
      this.showDialog = false
      // 数据清空
      this.roleForm = {
        name: '',
        description: ''
      }
      // 清楚校验痕迹
      this.$refs.roleForm.resetFields()
    },
    // 提交
    btnOK() {
      this.$refs.roleForm.validate(async(bool) => {
        if (bool) {
          if (this.roleForm.id) {
            await updateRole(this.roleForm)
          } else {
            await addRole(this.roleForm)
          }

          this.hgetRoleList()
          this.$message.success('操作成功')
          this.showDialog = false
        }
      })
    }
  }

}
</script>
